<?php
/**
 * Created by PhpStorm.
 * User: 六花
 * Date: 2019/5/16
 * Time: 16:17
 */

class limited_settingApp extends BackendApp{


    var $_limited_mod;

    function __construct()
    {
        $this->limited_settingApp();
    }

    function limited_settingApp()
    {
        parent::BackendApp();
        $this->_limited_mod =& m('limited_setting');
    }

    function index()
    {
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        if (!IS_POST)
        {
            $find_data = $this->_limited_mod->get('1=1');
            if (empty($find_data))
            {
                $this->show_warning('no_such_limitedtime');

                return;
            }
            if ($find_data['limited_url'])
            {
                $find_data['limited_url']           =   WEB_PHOTO_SRC . "/" . $find_data['limited_url'];
                $find_data['sort_url']              =   WEB_PHOTO_SRC . "/" . $find_data['sort_url'];
                $find_data['wish_url']              =   WEB_PHOTO_SRC . "/" . $find_data['wish_url'];
                $find_data['reduction_url']         =   WEB_PHOTO_SRC . "/" . $find_data['reduction_url'];
                $find_data['coupon_image']          =   WEB_PHOTO_SRC . "/" . $find_data['coupon_image'];
                $find_data['image_extension']       =   WEB_PHOTO_SRC . "/" . $find_data['image_extension'];
                $find_data['promote_image']         =   WEB_PHOTO_SRC . "/" . $find_data['promote_image'];
                $find_data['newpeople_image']       =   WEB_PHOTO_SRC . "/" . $find_data['newpeople_image'];
                $find_data['newpeoplefront_image']  =   WEB_PHOTO_SRC . "/" . $find_data['newpeoplefront_image'];
            }
            /* 显示新增表单 */
            $yes_or_no = array(
                1 => Lang::get('yes'),
                0 => Lang::get('no'),
            );
            $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js'
            ));
            $this->assign('yes_or_no', $yes_or_no);
            $this->assign('limited', $find_data);
            $this->display('limited_setting.form.html');
        }
        else
        {
            $find_data     = $this->_limited_mod->get('1=1');
            if (empty($find_data))
            {
                $this->show_warning('no_such_limitedtime');
                return;
            }
            //删除原图片
            if($_FILES['limited_url'] && $find_data['limited_url'] && $find_data['limited_url'] != $_FILES['limited_url']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['limited_url'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['limited_url']);
                }
            }
            if($_FILES['sort_url'] && $find_data['sort_url'] && $find_data['sort_url'] != $_FILES['sort_url']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['sort_url'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['sort_url']);
                }
            }
            if($_FILES['wish_url'] && $find_data['wish_url'] && $find_data['wish_url'] != $_FILES['wish_url']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['wish_url'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['wish_url']);
                }
            }
            if($_FILES['reduction_url'] && $find_data['reduction_url'] && $find_data['reduction_url'] != $_FILES['reduction_url']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['reduction_url'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['reduction_url']);
                }
            }
             if($_FILES['coupon_image'] && $find_data['coupon_image'] && $find_data['coupon_image'] != $_FILES['coupon_image']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['coupon_image'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['coupon_image']);
                }
            }
            if($_FILES['image_extension'] && $find_data['image_extension'] && $find_data['image_extension'] != $_FILES['image_extension']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['image_extension'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['image_extension']);
                }
            }
            if($_FILES['promote_image'] && $find_data['promote_image'] && $find_data['promote_image'] != $_FILES['promote_image']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['promote_image'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['promote_image']);
                }
            }
            if($_FILES['newpeople_image'] && $find_data['newpeople_image'] && $find_data['newpeople_image'] != $_FILES['newpeople_image']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['newpeople_image'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['newpeople_image']);
                }
            }
            if($_FILES['newpeoplefront_image'] && $find_data['newpeoplefront_image'] && $find_data['newpeoplefront_image'] != $_FILES['newpeoplefront_image']){
                if(file_exists(WEB_PHOTO_SRC . '/' . $find_data['newpeoplefront_image'])){
                    unlink(WEB_PHOTO_SRC . '/' . $find_data['newpeoplefront_image']);
                }
            }
            $data = array(
                'limited_title' => $_POST['limited_title'],
            );
            $logo =   $this->_upload_logo();
            if(!empty($logo) && !empty($logo['limited_url'])){
                $data['limited_url'] = $logo['limited_url'];
            }
            if(!empty($logo) && !empty($logo['sort_url'])){
                $data['sort_url'] = $logo['sort_url'];
            }
            if(!empty($logo) && !empty($logo['wish_url'])){
                $data['wish_url'] = $logo['wish_url'];
            }
            if(!empty($logo) && !empty($logo['reduction_url'])){
                $data['reduction_url'] = $logo['reduction_url'];
            }
             if(!empty($logo) && !empty($logo['coupon_image'])){
                $data['coupon_image'] = $logo['coupon_image'];
            }
            if(!empty($logo) && !empty($logo['image_extension'])){
                $data['image_extension'] = $logo['image_extension'];
            }
            if(!empty($logo) && !empty($logo['promote_image'])){
                $data['promote_image'] = $logo['promote_image'];
            }
            if(!empty($logo) && !empty($logo['newpeople_image'])){
                $data['newpeople_image'] = $logo['newpeople_image'];
            }
            if(!empty($logo) && !empty($logo['newpeoplefront_image'])){
                $data['newpeoplefront_image'] = $logo['newpeoplefront_image'];
            }
            if ($logo === false)
            {
                return;
            }
            $rows=$this->_limited_mod->edit($find_data['limited_id'], $data);
            if ($this->_limited_mod->has_error())
            {
                $this->show_warning($this->_limited_mod->get_error());
                return;
            }else{
                $this->admin_log("修改手机广告[".$_POST['ad_name']."]",1);
            }

            $this->show_message('edit_limited_successed',
                'back_list',        'index.php?app=limited_setting&amp;act=index&amp;id='. $find_data['limited_id'],
                'edit_again',    'index.php?app=limited_setting&amp;act=index&amp;id=' . $find_data['limited_id']);
        }


    }

    function _upload($file,$str){
        require_once '../includes/aliyunOss/samples/Common.php';
        $image = $file[$str];
        $imageName = date("YmdHis", time()) . rand(1000,10000);
        $path = 'data/files/mall/limited_setting/'.$str;

        if ($image['error'] == UPLOAD_ERR_NO_FILE) { // 没有文件被上传
            return '';
        }
        import('uploader.lib');             //导入上传类
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES[$str]); //上传logo
        if (!$uploader->file_info()) {
            $this->show_warning($uploader->get_error(), 'go_back', 'index.php?app=limited_setting&amp;act=index');
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);
        if (!$file_path = $uploader->save($path,$imageName)) {   //保存到指定目录，并以指定文件名$ad_id存储
            $this->show_message('上传文件失败！');
            return false;
        }
        $ossClient = new \common;
        $r = $ossClient->putObjectFile('../'.$file_path,$file_path);
        if(!$r['info'] || $r['info']['http_code'] != 200){
            $this->show_message('上传oss失败！');
            return false;
        }
        unlink('../'.$file_path);
        return $file_path;
    }

    function _upload_logo() {
        $file = $_FILES;
        $res = array();
        foreach($file as $k=> $v){
            if ($v['error'] == UPLOAD_ERR_OK) { // 没有文件被上传
                $res[$k] = $this->_upload($v,$k);
            }
        }
        return $res;
    }

}